#include "core.h"

struct option_list_t *p_user_list = NULL;
int s_userlist_num = 0;

option_parse_func_t user_func = NULL;

extern int user_func_register(void);

int create_user_option_list(struct option_list_t *list,int list_num)
{
    if( list )
    {
        p_user_list = list;
        s_userlist_num = list_num;
    }
    else
    {
        return 0;
    }
    return list_num;
}

int create_user_option_parse(option_parse_func_t func)
{
    if(func)
    {
        user_func = func;
    }
    else
    {
        return -1;
    }
    return 0;
}

int main(int argc,char *argv[])
{
    int option_count = argc - 1;
    int i,j;
    int option_found = 0;
    int ret = 0;

    user_func_register();

    if( p_user_list == NULL )
    {
        return -1;
    }

    if( user_func == NULL )
    {
        return -2;
    }

    for(i = 0 ; i < s_userlist_num ; i++)
    {
        for(j = 1 ; j <= option_count ; j++)
        {
            option_found = 0;
            if( strcmp(p_user_list[i].name,argv[j]) == 0)
            {
                if( user_func )
                {
                    //超出选项就返回
                    if( j+1 > option_count)
                    {
                        return 1;
                    }
                    user_func(&p_user_list[i],argv[j+1]);

                }
                option_found = 1;
            }

            if( option_found )
            {
                break;
            }
        }
        
    }

    return 0;
}
